import React from 'react';
import { toJS } from 'mobx';
import { inject, observer } from 'mobx-react';
import { Button,Tooltip} from 'antd'
import { WeaAuth, WeaPopoverHrm,WeaLocaleProvider, WeaTableEditable, WeaDialog, WeaRightMenu, WeaTop } from "ecCom";
import { WeaTableNew } from "comsMobx";
const WeaTable = WeaTableNew.WeaTable;
const getLabel = WeaLocaleProvider.getLabel;
@inject("goalOrgShareStore","goalCommonStore")
@observer
export default class GoalOrgShare extends React.Component{
    constructor(props) {
        super(props);
        this.store = this.props.goalOrgShareStore;
        this.state = {
            tableSelectedRowKeys: [],
            showOperation: false, //显示操作框
            ids:'',//勾选删除的选项
            btnType:"disabled",
            shareData:{
                id:'',
                itemid:'',   //对象id
                itemtype:'', //对象类型
                sharetype:'', //共享类型
                shareid:'',   //共享id
                hassub:'',    //是否包含下级
                fromlevel:'', //安全级别 from
                tolevel:'',    //安全级别 to
                postlevel:'', //岗位级别
                targetobjectid:'', //指定级别对应分部或者部门的id
                rolelevel:'',   //角色级别
            },
            rightMenu: [
                {
                  key: "0",
                  icon: <i className="icon-coms-Save-as" />,
                  content: getLabel(365,'新建'),
                  onClick: () => console.log("新建")
                },
                {
                  key: "1",
                  disabled:"true",
                  icon: <i className="icon-coms-Save-as" />,
                  content: getLabel(32136,'批量删除'),
                  onClick: () =>  console.log("批量删除")
                }
              ], 
        };
    }

    componentWillMount() {
        //一些初始化请求
        // this.goalCateSetting({operate:'query'})
        this.goalOrgShareList({});
    }

    /**
     * 获取任务分享列表
     */
    goalOrgShareList = (params) => {
        const {goalOrgShareStore,goalCommonStore:{typeId,orgId}} = this.props;
        params.typeId = typeId;
        params.orgId = orgId;
        goalOrgShareStore.goalOrgShareList(params);
    }

    
    onSelectChange = (selectedKeys, selectedRows) => {
      let {rightMenu} = this.state;
      let keys = toJS(selectedKeys);
      let len = keys.length;
      let ids = "";
      for(let i = 0;i < len;i ++){
          ids += keys[i] + ",";
      }
      ids = ids.substring(0, ids.length - 1);
      if(ids.length > 0){
        this.setState({ids,btnType:""})
      }else{
        this.setState({ids,btnType:"disabled"})
      }
    }

    //列表右侧操作
    onOperatesClick = (record, index, operate, flag) => {
      let params = {doback:()=>this.goalOrgShareList({})};
      if(flag == 0){
        params.id = record.id;
        params.operate = "del";
      }
      this.store.goalOrgShareSetting(params);

    }

    //添加分享
    addShare = () =>{
        this.setState({ showOperation: true });
    }

    //保存
    onSave = (data) => {
        const {goalOrgShareStore, goalCommonStore:{typeId,orgId}} = this.props;
        this.setState({ showOperation: false });
        let params = {
            sharetype:data.sharetype,
            operate:"add",
            typeId,
            orgId
        };

        if(data.sharetype == 1){ //人力资源
            params.shareid = data.userid;
        }else if(data.sharetype == 2){//分部
            params.shareid = data.subid;
            params.hassub = data.hassub;
            params.fromlevel = data.fromlevel;
            params.tolevel = data.tolevel;
        }else if(data.sharetype == 3){//部门
            params.shareid = data.deptid;
            params.hassub = data.hassub;
            params.fromlevel = data.fromlevel;
            params.tolevel = data.tolevel;
        }else if(data.sharetype == 10){ //岗位
            params.shareid = data.objid;
            params.postlevel = data.postlevel;
            if(data.postlevel != 1){
              params.targetobjectid = data.targetobjectid;
            }
        }else if(data.sharetype == 4){ //角色
            params.shareid = data.objid;
            params.rolelevel = data.rolelevel;
            params.fromlevel = data.fromlevel;
            params.tolevel = data.tolevel;
        }else{
          params.shareid = -1;
          params.fromlevel = data.fromlevel;
          params.tolevel = data.tolevel;
        }
        params.doback = ()=>this.goalOrgShareList({});
        goalOrgShareStore.goalOrgShareSetting(params);
    }

    //任务共享的编辑，批量删除
    goalOrgShareSetting = ()=>{
      let {ids} = this.state;
      let params = {};
      params.ids = ids;
      params.operate = "batch";
      params.doback = ()=>{
        this.setState({ids,btnType:"disabled"})
        this.goalOrgShareList({})
      }
      this.store.goalOrgShareSetting(params);
      
    }


    getRightMenu = ()=>{
      let {btnType} = this.state; 
      return [
        {
          key: "0",
          icon: <i className="icon-coms-Save-as" />,
          content: getLabel(365,'新建'),
          onClick: () => this.addShare
        },
        btnType == "disabled"?
        {
          key: "1",
          disabled:"true",
          icon: <i className="icon-coms-Save-as" />,
          content: getLabel(32136,'批量删除'),
          onClick: () =>  console.log("批量删除")
        }:
        {
          key: "1",
          disabled:"false",
          icon: <i className="icon-coms-Save-as" />,
          content: getLabel(32136,'批量删除'),
          onClick: () => this.goalOrgShareSetting
        }
      ]
    }



    render() {
        const {goalOrgShareStore} = this.props;
        const {shareTable} = goalOrgShareStore;
        const { tableSelectedRowKeys, rightMenu, btns, showOperation, btnType} = this.state;
        return (
          <div className="wea-workrelate-share" style={{ height: 400 }}>
            <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@o5s8iq`}
              datas={rightMenu} // 右键菜单
            >
              <div className="share-title-div">
                <div className="share-title-div-button">
                  <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@pm5g2z`} type="primary" className="share-button" title={getLabel(611, '添加')} onClick={this.addShare}><span className="icon-coms-Add-to-hot"></span></Button>
                  {btnType == "disabled"?<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@3tq3pd`} type="primary" className="share-button" disabled={true} title={getLabel(91, '删除')} onClick={this.goalOrgShareSetting}><span className="icon-coms-form-delete-hot"></span></Button>:<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@x4885b`} type="primary" className="share-button" title={getLabel(91, '删除')} onClick={this.goalOrgShareSetting}><span className="icon-coms-form-delete-hot"></span></Button>}
                </div>
              </div>

              <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@fd3v5u`} // table内部做了loading加载处理，页面就不需要再加了
                comsWeaTableStore={shareTable} // table store
                hasOrder={true} // 是否启用排序
                needScroll={true} // 是否启用table内部列表滚动，将自适应到父级高度
                rowSelection={{
                  selectedRowKeys: tableSelectedRowKeys,
                  onChange: this.onSelectChange
                }}
                onOperatesClick={this.onOperatesClick} //右键菜单操作

              />

            </WeaRightMenu>

            <WeaAuth ecId={`${this && this.props && this.props.ecId || ''}_WeaAuth@w9b21f`}
              visible={showOperation}
              conditions={this.getCondition()}
              title='添加成员'
              onOk={(data) => {
                // operationShare(data);
                this.onSave(data)

              }}
              onCancel={() => {
                this.setState({ showOperation: false });
              }}
            />

          </div>)
    }

     //获取条件
     getCondition() {
        return [{
            "colSpan": 2,
            "conditionType": "SELECT",
            "domkey": ["sharetype"],
            "fieldcol": 18,
            "isQuickSearch": false,
            "label": "对象类型",
            "labelcol": 6,
            "options": [
                { "key": "1", "selected": true, "showname": "人力资源" },
                { "key": "2", "selected": false, "showname": "分部" },
                { "key": "3", "selected": false, "showname": "部门" },
                  {
                    "key": "10",
                    "selected": false,
                    "showname": "岗位"
                  },
                  , {
                    "key": "4",
                    "selected": false,
                    "showname": "角色"
                  },
                { "key": "5", "selected": false, "showname": "所有人" },
            ],
            "viewAttr": 2
        }, {
            "1": [[{
                "browserConditionParam": {
                    "replaceDatas": [],
                    "completeParams": {},
                    "conditionDataParams": {},
                    "dataParams": {},
                    "destDataParams": {},
                    "hasAddBtn": false,
                    "hasAdvanceSerach": true,
                    "idSeparator": ",",
                    "isAutoComplete": 1,
                    "isDetail": 0,
                    "isMultCheckbox": false,
                    "isSingle": false,
                    "pageSize": 10,
                    "quickSearchName": "",
                    "type": "17",
                    "viewAttr": 3
                },
                "colSpan": 2,
                "conditionType": "BROWSER",
                "domkey": ["userid"],
                "fieldcol": 18,
                "isQuickSearch": false,
                "label": "对象",
                "labelcol": 6,
                "viewAttr": 3
            }]],
            "2": [[{
                "browserConditionParam": {
                    "completeParams": {},
                    "conditionDataParams": {},
                    "dataParams": {},
                    "destDataParams": {},
                    "hasAddBtn": false,
                    "hasAdvanceSerach": true,
                    "idSeparator": ",",
                    "isAutoComplete": 1,
                    "isDetail": 0,
                    "isMultCheckbox": false,
                    "isSingle": false,
                    "pageSize": 10,
                    "quickSearchName": "",
                    "type": "194",
                    "viewAttr": 3
                },
                "colSpan": 2,
                "conditionType": "BROWSER",
                "domkey": ["subid"],
                "fieldcol": 18,
                "isQuickSearch": false,
                "label": "对象",
                "labelcol": 6,
                "value": 3,
                "viewAttr": 2
            }, {
                "colSpan": 2,
                "conditionType": "CHECKBOX",
                "domkey": ["hassub"],
                "fieldcol": 18,
                "isQuickSearch": false,
                "label": "含下级",
                "labelcol": 6,
                "viewAttr": 2
            }], [{
                "colSpan": 2,
                "conditionType": "INPUT_INTERVAL",
                "domkey": ["fromlevel", "tolevel"],
                "fieldcol": 18,
                "isQuickSearch": false,
                "label": "安全级别",
                "labelcol": 6,
                "value": [10, 100],
                "viewAttr": 2
            }]],
            "3": [[{
                "browserConditionParam": {
                    "completeParams": {},
                    "conditionDataParams": {},
                    "dataParams": {},
                    "destDataParams": {},
                    "hasAddBtn": false,
                    "hasAdvanceSerach": true,
                    "idSeparator": ",",
                    "isAutoComplete": 1,
                    "isDetail": 0,
                    "isMultCheckbox": false,
                    "isSingle": false,
                    "pageSize": 10,
                    "quickSearchName": "",
                    "type": "57",
                    "viewAttr": 3
                },
                "colSpan": 2,
                "conditionType": "BROWSER",
                "domkey": ["deptid"],
                "fieldcol": 18,
                "isQuickSearch": false,
                "label": "对象",
                "labelcol": 6,
                "viewAttr": 3
            }, {
                "colSpan": 2,
                "conditionType": "CHECKBOX",
                "domkey": ["hassub"],
                "fieldcol": 18,
                "isQuickSearch": false,
                "label": "含下级",
                "labelcol": 6,
                "viewAttr": 2
            }], [{
                "colSpan": 2,
                "conditionType": "INPUT_INTERVAL",
                "domkey": ["fromlevel", "tolevel"],
                "fieldcol": 18,
                "isQuickSearch": false,
                "label": "安全级别",
                "labelcol": 6,
                "value": [10, 100],
                "viewAttr": 2
            }]],
            "4": [
                [{
                  "browserConditionParam": {
                    "completeParams": {},
                    "conditionDataParams": {},
                    "dataParams": {},
                    "destDataParams": {},
                    "hasAddBtn": false,
                    "hasAdvanceSerach": true,
                    "idSeparator": ",",
                    "isAutoComplete": 1,
                    "isDetail": 0,
                    "isMultCheckbox": false,
                    "isSingle": false,
                    "pageSize": 10,
                    "quickSearchName": "",
                    "type": "65",
                    "viewAttr": 3
                  },
                  "colSpan": 2,
                  "conditionType": "BROWSER",
                  "domkey": ["objid"],
                  "fieldcol": 18,
                  "isQuickSearch": false,
                  "label": "对象",
                  "labelcol": 6,
                  "viewAttr": 3
                }, {
                  "colSpan": 2,
                  "conditionType": "SELECT",
                  "domkey": ["rolelevel"],
                  "fieldcol": 18,
                  "isQuickSearch": false,
                  "label": "级别",
                  "labelcol": 6,
                  "options": [{
                    "key": "0",
                    "selected": true,
                    "showname": "总部"
                  }, {
                    "key": "1",
                    "selected": false,
                    "showname": "分部"
                  }, {
                    "key": "2",
                    "selected": false,
                    "showname": "部门"
                  }],
                  "viewAttr": 2
                }],
                [{
                  "colSpan": 2,
                  "conditionType": "INPUT_INTERVAL",
                  "domkey": ["fromlevel", "tolevel"],
                  "fieldcol": 18,
                  "isQuickSearch": false,
                  "label": "安全级别",
                  "labelcol": 6,
                  "value": [10, 100],
                  "viewAttr": 2
                }],
              ],
            "10": [
                [{
                  "browserConditionParam": {
                    "completeParams": {},
                    "conditionDataParams": {},
                    "dataParams": {},
                    "destDataParams": {},
                    "hasAddBtn": false,
                    "hasAdvanceSerach": true,
                    "idSeparator": ",",
                    "isAutoComplete": 1,
                    "isDetail": 0,
                    "isMultCheckbox": false,
                    "isSingle": false,
                    "pageSize": 10,
                    "quickSearchName": "",
                    "type": "278",
                    "viewAttr": 3
                  },
                  "colSpan": 2,
                  "conditionType": "BROWSER",
                  "domkey": ["objid"],
                  "fieldcol": 18,
                  "isQuickSearch": false,
                  "label": "对象",
                  "labelcol": 6,
                  "viewAttr": 3
                }],
                [{
                  "colSpan": 2,
                  "conditionType": "SELECT_LINKAGE",
                  "domkey": ["postlevel"],
                  "fieldcol": 18,
                  "isQuickSearch": false,
                  "label": "岗位级别",
                  "labelcol": 6,
                  "options": [{
                    "key": "1",
                    "selected": true,
                    "showname": "总部"
                  }, {
                    "key": "2",
                    "selected": false,
                    "showname": "指定分部"
                  }, {
                    "key": "3",
                    "selected": false,
                    "showname": "指定部门"
                  }],
                  "selectLinkageDatas": {
                    "3": {
                      "browserConditionParam": {
                        "completeParams": {},
                        "conditionDataParams": {},
                        "dataParams": {},
                        "destDataParams": {},
                        "hasAddBtn": false,
                        "hasAdvanceSerach": true,
                        "idSeparator": ",",
                        "isAutoComplete": 1,
                        "isDetail": 0,
                        "isMultCheckbox": false,
                        "isSingle": false,
                        "pageSize": 10,
                        "quickSearchName": "",
                        "type": "57",
                        "viewAttr": 3
                      },
                      "colSpan": 2,
                      "conditionType": "BROWSER",
                      "domkey": ["targetobjectid"],
                      "fieldcol": 18,
                      "isQuickSearch": false,
                      "label": "",
                      "labelcol": 6,
                      "viewAttr": 3
                    },
                    "2": {
                      "browserConditionParam": {
                        "completeParams": {},
                        "conditionDataParams": {},
                        "dataParams": {},
                        "destDataParams": {},
                        "hasAddBtn": false,
                        "hasAdvanceSerach": true,
                        "idSeparator": ",",
                        "isAutoComplete": 1,
                        "isDetail": 0,
                        "isMultCheckbox": false,
                        "isSingle": false,
                        "pageSize": 10,
                        "quickSearchName": "",
                        "type": "194",
                        "viewAttr": 3
                      },
                      "colSpan": 2,
                      "conditionType": "BROWSER",
                      "domkey": ["targetobjectid"],
                      "fieldcol": 18,
                      "isQuickSearch": false,
                      "label": "",
                      "labelcol": 6,
                      "viewAttr": 3
                    }
                  },
                  "viewAttr": 2
                }]
              ],
              "5": [[{
                "colSpan": 2,
                "conditionType": "INPUT_INTERVAL",
                "domkey": ["fromlevel", "tolevel"],
                "fieldcol": 18,
                "isQuickSearch": false,
                "label": "安全级别",
                "labelcol": 6,
                "value": [10, 100],
                "viewAttr": 2
            }]],

        }]
    }
}